package com.cn.shiro.zy.shiro.controller;

import com.cn.shiro.zy.shiro.entity.User;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Objects;

/**
 * @author zhangyi
 * @date 2018/12/13 17:56
 */
@Controller
public class BaseController {

    @ModelAttribute
    public User getUser(){
        Object subject = SecurityUtils.getSubject().getPrincipal();
        if(!Objects.isNull(subject)){
            if(subject instanceof User){
                System.out.println("获取用户信息:"+((User) subject).getUserName()+"--"+((User) subject).getPassword());
                return  (User) subject;
            }

        }
            return null;
    }
}
